﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Constants;
using mshtml;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class SendLetunToBigAdventureChainItem : BotvaLogedInChainItemBase
    {
        public int GildiaId { get; set; }

        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/castle.php";
            Method = HttpMethod.POST;
            AddGet("a", "zoo");
            AddGet("id", GildiaId.ToString());

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != "FORM")
                    continue;

                if (item.getAttribute("action", 0).ToString() != "")
                    continue;

                IHTMLElementCollection inputs = (item.children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;

                bool isFormCorrect = false;
                
                foreach (IHTMLElement input in inputs)
                {
                    if(!(input is IHTMLInputElement))
                        continue;
                    
                    IHTMLInputElement inp = input as IHTMLInputElement;
                    if (inp.type.Trim().ToUpper() == "SUBMIT" && inp.value.Trim() == "ОТПРАВИТЬ" && input.className.Trim() == "CMD_ALL CMD_ROW4 CMD_AROW4")
                    {
                        isFormCorrect = true;
                    }

                    if (isFormCorrect)
                        break;
                }

                if (!isFormCorrect)
                    continue;

                List<IHTMLInputElement> inps = new List<IHTMLInputElement>(inputs.Cast<IHTMLInputElement>());
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "K").Count() > 0;
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "BLOCK").Count() > 0;
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "DO_CMD").Count() > 0;
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "FLYING").Count() > 0;

                if (!isFormCorrect)
                    continue;

                foreach (IHTMLElement input in inputs)
                {
                    IHTMLInputElement inp = input as IHTMLInputElement;

                    if (inp.name == null || inp.name.Trim().Length == 0)
                        continue;

                    AddPost(input.getAttribute("NAME", 0).ToString().ToLower(), input.getAttribute("VALUE", 0).ToString().ToLower());
                }
            }
        }
    }
}

/*
http://g3.botva.ru/castle.php?a=zoo&id=8

block	events
do_cmd	do_big
flying	1333
k	58840

<form method="post" action="">
<input type="hidden" value="58840" name="k">
<input type="hidden" value="events" name="block">
<input type="hidden" value="do_big" name="do_cmd">
<input type="hidden" value="1333" name="flying">
<input class="cmd_all cmd_row4 cmd_arow4 " type="submit" value="ОТПРАВИТЬ">
</form>
*/